<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Logic;

class ArticleCrawlerLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $articleCrawlerModel;

    public function _initialize()
    {
        $this->articleCrawlerModel = D('ArticleCrawler', 'Model');
    }


    /**
     * 获取文章采集列表
     *
     * @param number $page
     * @param number $size
     * @param array  $params
     *
     * @return array
     */
    public function getArticleCrawlerList($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 10 : (int)$size;

        if (isset($params['status'])) {
            $map['status'] = $params['status'];
        }
        if ($params['article_type']) {
            $map['type'] = $params['article_type'];
        }
        if ($params['article_from']) {
            $map['from_name'] = $params['article_from'];
        }
        if ($params['category_id']) {
            $map['category_id'] = $params['category_id'];
        }
        if ($params['child_id']) {
            $map['child_id'] = $params['child_id'];
        }

        if (isset($params['is_top'])) {
            $map['is_top'] = $params['is_top'];
        }

        if ($params['article_title']) {
            $map['article_title'] = ['like', "%{$params['article_title']}%"];
        }
        $startTime = strtotime($params['start_time']);
        $endTime = '';
        if (!empty($params['end_time'])) {
            $endTime = strtotime($params['end_time'] . ' 23:59:59');
        }

        if (!empty($startTime) && !empty($endTime)) {
            $map['publish_time'] = ['between', [$startTime, $endTime]];
        } elseif (!empty($startTime)) {
            $map['publish_time'] = ['gt', $startTime];
        } else if (!empty($endTime)) {
            $map['publish_time'] = ['lt', $endTime];
        }

        $cols = 'article_id,category_id,child_id,article_title,author,is_top,status,
        create_time,publish_time,cron_time,type,from_name,from_url';
        $total = $this->articleCrawlerModel->field($cols)->where($map)->count();
        $order = '`article_id` DESC';
        $list = $this->articleCrawlerModel->field($cols)
            ->where($map)
            ->page($page)
            ->limit($size)
            ->order($order)
            ->select();
        $result = [
            'list'  => $list,
            'total' => $total
        ];
        return $result;
    }

    /**
     * 获取文章内容
     *
     * @param int $articleId
     */
    public function getArticleCrawlerById($articleId = 0)
    {
        if (empty($articleId)) {
            return false;
        }
        $condition = [];
        $condition['article_id'] = ['eq', $articleId];
        return $this->articleCrawlerModel->where($condition)->find();
    }

    /**
     * 保存文章
     *
     * @param array $data
     *
     * @return boolean|mixed|boolean|unknown|string
     */
    public function saveArticleCrawler($data = [])
    {
        if (empty($data) || empty($data['id'])) {
            return false;
        }
        $articleId = $data['id'];
        unset($data['id']);
        $data['update_time'] = NOW_TIME;
        return $this->articleCrawlerModel->where(['article_id' => $articleId])->save($data);
    }
}
